﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_Dispatch : System.Web.UI.Page
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        ZSql zs = new ZSql();
        if (!IsPostBack)
        {
            lblDh.Text = initBarCode();
            lblPath.Text = f.getModuleidAdd(Request.QueryString["moduid"]);
            hidModuid.Value = Request.QueryString["moduid"];
            initUser();
            intData();//修改时读取数据
        }
    }
    //提交保存
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        ZSql info = new ZSql();
        string id = Request.QueryString["id"];
        string sql = "";
        if (string.IsNullOrEmpty(id))//新增
            sql = "select top 1 * from CS_Mailadd";
        else//修改
            sql = "select top 1 * from CS_Mailadd where Maintaddid=" + id;

        info.Open(sql);
        info.NextRow();
        if (string.IsNullOrEmpty(id))//新增
        {
            info.NewRow();
        }
        info["MaintCode"] = lblDh.Text;//维修编号
        info["Guestname"] = txtGuestName.Text;//客户名称
        info["Guestmen"] = txtManName.Text;//联系人
        info["Guestaddress"] = txtAddress.Text;//联系地址
        info["Guesttel"] = txtPhone.Text;//联系电话   
        info["MaintDate"] = txtMaintDate.Text;
        info["dispTime"] = txtDispTime.Text;//购买时间
        info["comitype"] = txtcomitype.Text;//型号类型
        info["comidband"] = txtBrand.Text;//品牌
        info["comidSpe"] = txtSpeci.Text;//型号
        info["MaintTrouble"] = txtMaintTrouble.Text;//报修故障
        info["maintmen"] = ddlName.SelectedValue;//维修人Id
        info["BespeakDate"] = txtBespeakDate.Text;//预约时间        
        info["sjfj"] = txtsjfj.Text;//随即附件
        info["xlh2"] = txtxlh2.Text;//序列号1
        info["xlh1"] = txtxlh1.Text;//序列号2
        info["AuditState"] = "已通过";
        info["EditUserName"] = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//编辑人
        info["EditDate"] = DateTime.Now.ToString();//时间

        info.Update();
        info.Close();
        Response.Write("<script>alert('操作已成功，请返回！');</script>");//返回页面
        Response.Write("<script>window.location='ServiceGuest.aspx?moduid=" + hidModuid.Value + "';</script>");//返回页面
    }


    //生成编码
    private string initBarCode()
    {
        ZSql zs = new ZSql();
        string id = "wx" + DateTime.Now.ToString("yyyyMMdd") + "-";
        zs.Open("select max(Maintaddid) as a from CS_Mailadd");
        if (string.IsNullOrEmpty(zs.m_table.Rows[0]["a"].ToString()))
        {
            id += 1;
        }
        else
        {
            id += Convert.ToInt32(zs.m_table.Rows[0]["a"]) + 1;
        }

        return id;

    }
    //生成用户
    private void initUser()
    {
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=40 order by StaffName");
        ddlName.DataSource = name.m_table;
        ddlName.DataTextField = "StaffName";
        ddlName.DataValueField = "StaffId";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("—选派员工—", ""));
        name.Close();
    }

    private void intData()
    {
        string id = Request.QueryString["id"];
        if (!string.IsNullOrEmpty(id))//新增
        {
            ZSql zs = new ZSql();
            zs.Open("select * from CS_Mailadd where Maintaddid=" + Request.QueryString["id"]);

            lblDh.Text = zs.m_table.Rows[0]["MaintCode"].ToString();
            txtGuestName.Text = zs.m_table.Rows[0]["Guestname"].ToString();
            txtManName.Text = zs.m_table.Rows[0]["Guestmen"].ToString();
            txtAddress.Text = zs.m_table.Rows[0]["Guestaddress"].ToString();
            txtPhone.Text = zs.m_table.Rows[0]["Guesttel"].ToString();
            txtMaintDate.Text = zs.m_table.Rows[0]["MaintDate"].ToString();
            txtDispTime.Text = zs.m_table.Rows[0]["dispTime"].ToString();
            txtcomitype.Text = zs.m_table.Rows[0]["comitype"].ToString();
            txtBrand.Text = zs.m_table.Rows[0]["comidband"].ToString();
            txtSpeci.Text = zs.m_table.Rows[0]["comidSpe"].ToString();
            txtMaintTrouble.Text = zs.m_table.Rows[0]["MaintTrouble"].ToString();
            ddlName.SelectedValue = zs.m_table.Rows[0]["maintmen"].ToString();
            txtBespeakDate.Text = zs.m_table.Rows[0]["BespeakDate"].ToString();
            txtsjfj.Text = zs.m_table.Rows[0]["sjfj"].ToString();
            txtxlh1.Text = zs.m_table.Rows[0]["xlh1"].ToString();
            txtxlh2.Text = zs.m_table.Rows[0]["xlh2"].ToString();

            zs.Close();
        
        }
    }
}
